package com.pro_one.controller;

import com.pro_one.constant.MessageConstant;
import com.pro_one.entity.PageResult;
import com.pro_one.entity.QueryPageBean;
import com.pro_one.entity.Result;
import com.pro_one.pojo.Menu;
import com.pro_one.service.MenuService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 动态菜单功能实现
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    /**
     * 根据用户动态展示菜单
     *
     * @param username
     * @return
     */
    @RequestMapping("/getMenu")
    public Result getMenu(String username) {
        try {
            List<Map> list = menuService.getMenuByUsername(username);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }

    }

    /**
     * 添加菜单
     * @param menu 菜单对象
     * @return
     */
    @RequestMapping("/addMenu")
    @PreAuthorize("hasAuthority('MENU_ADD')")
    public Result addMenu(@RequestBody Menu menu) {
        try {
            menuService.addMenu(menu);
            return new Result(true,MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MENU_FAIL);
        }


    }

    /**
     * 删除菜单
     * @param id 菜单id
     * @return
     */
    @RequestMapping("/deleteMenu")
    @PreAuthorize("hasAuthority('MENU_DELETE')")
    public Result deleteMenu(Integer id) {
        try {
            menuService.deleteMenu(id);
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MENU_FAIL);
        }


    }

    /**
     * 修改菜单
     * @param menu
     * @return
     */
    @RequestMapping("/updateMenu")
    @PreAuthorize("hasAuthority('MENU_EDIT')")
    public Result updateMenu(@RequestBody Menu menu) {
        try {
            menuService.updateMenu(menu);
            return new Result(true,MessageConstant.UPDATE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.UPDATE_MENU_FAIL);
        }


    }

    /**
     * 查询所有菜单
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findAllMenu")
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public PageResult findAllMenu(@RequestBody QueryPageBean queryPageBean) {
            PageResult menus = menuService.findAllMenu(queryPageBean);
            return menus;
    }

    /**
     * 查询所有顶级菜单
     * @return 返回值包含name和id
     */
    @RequestMapping("/findParentMenuId")
    public List<Map> findParentMenuId() {
        List<Map> parents = menuService.findParentMenuId();
        return parents;
    }

    /**
     * 根据角色id查询所有菜单，主要用来把勾选的菜单排在前面，和查询所有菜单查不出
     * @param queryPageBean
     * @param roleId
     * @return
     */
    @RequestMapping("/findAllMenuByRoleId")
    public PageResult findAllMenuByRoleId(@RequestBody QueryPageBean queryPageBean,Integer roleId) {
        PageResult menus = menuService.findAllMenuByRoleId(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(),roleId);
        return menus;
    }

    /**
     * 根据角色id查询所有与之关联的菜单的id
     * @param roleId
     * @return
     */
    @RequestMapping("/findMenuIdsByRoleId")
    public Integer[] findMenuIdsByRoleId(Integer roleId) {
        Integer[] menuIdsByRoleId = menuService.findMenuIdsByRoleId(roleId);
        return menuIdsByRoleId;
    }

    /**
     * 根据顶级菜单id查询所有与之关联的子菜单，用来在前台展示
     * @param id
     * @return
     */
    @RequestMapping("/findAllByParentId")
    public List<Menu> findAllByParentId(Integer id) {
        List<Menu>allByParentId = menuService.findAllByParentId(id);
        return allByParentId;
    }

}
